package com.support.android.myapp3.acitivty;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.support.android.myapp3.R;
import com.support.android.myapp3.adapter.DrawerListAdapter;
import com.support.android.myapp3.bean.ItemModelOfDrawerList;
import com.support.android.myapp3.fragment.CatagoryFragment;
import com.support.android.myapp3.fragment.DiscoverFragment;
import com.support.android.myapp3.fragment.FinderFragment;
import com.support.android.myapp3.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class DrawActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.drawer_layout)
    DrawerLayout mDrawerLayout;
    private List<ItemModelOfDrawerList> items = new ArrayList<>();

    @BindView(R.id.ll_drawer)
    LinearLayout ll_drawer;
    @BindView(R.id.lv_drawer)
    ListView lv_drawer;


    private FragmentManager fragmentManager;
    private long firstPressTime = 0;
    private Fragment currentFragment;
    private int currentFragmentId = 0;

    @Override
    public int getLayoutView() {
        return R.layout.layout_maindraw;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        unbinder = ButterKnife.bind(this);
        initView(savedInstanceState);
    }

    private Unbinder unbinder;

    @Override
    protected void onDestroy() {
        unbinder.unbind();
        super.onDestroy();
    }

    private void initView(Bundle savedInstanceState) {


        // 主页面默认添加NewsFragment
        fragmentManager = getSupportFragmentManager();

        /**
         * 防止碎片重叠
         */
        Fragment videoFragment;
        Fragment weatherFragment;
        Fragment newsFragment;
        if (savedInstanceState != null) {
            currentFragment = fragmentManager.findFragmentByTag("apple");
            newsFragment = fragmentManager.findFragmentByTag("news");
            videoFragment = fragmentManager.findFragmentByTag("video");
            weatherFragment = fragmentManager.findFragmentByTag("weather");
            fragmentManager.beginTransaction().show(currentFragment)
                    .hide(newsFragment)
                    .hide(videoFragment)
                    .hide(weatherFragment)
                    .commit();

        } else {
            currentFragment = new HomeFragment();
            newsFragment = new CatagoryFragment();
            videoFragment = new DiscoverFragment();
            weatherFragment = new FinderFragment();
            fragmentManager.beginTransaction().add(R.id.ll_content, currentFragment, "apple")
                    .add(R.id.ll_content, videoFragment, "video")
                    .add(R.id.ll_content, weatherFragment, "weather")
                    .add(R.id.ll_content, newsFragment, "news")
                    .show(currentFragment)
                    .hide(newsFragment)
                    .hide(videoFragment)
                    .hide(weatherFragment)
                    .commit();
        }


        setSupportActionBar(toolbar);
        // 这句话保证title能被修改
        getSupportActionBar().setDisplayShowTitleEnabled(false);
//        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("苹果");

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        items.add(new ItemModelOfDrawerList(R.mipmap.apple, "苹果"));
        items.add(new ItemModelOfDrawerList(R.mipmap.news_icon, "新闻"));
        items.add(new ItemModelOfDrawerList(R.mipmap.video_icon, "视频"));
        items.add(new ItemModelOfDrawerList(R.mipmap.weather_icon, "天气"));

        DrawerListAdapter mAdapter = new DrawerListAdapter(this, items);
        lv_drawer.setAdapter(mAdapter);

        lv_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switchFragment(position);
            }
        });

        ActionBarDrawerToggle mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

        mToggle.syncState();
        mDrawerLayout.addDrawerListener(mToggle);
    }

    public void switchFragment(int fragmentId) {
        mDrawerLayout.closeDrawer(ll_drawer);
        if (currentFragmentId == fragmentId)
            return;
        currentFragmentId = fragmentId;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        Fragment toFragment = null;
        switch (fragmentId) {
            case 0:
                toFragment = fragmentManager.findFragmentByTag("apple");
                toolbar.setTitle("苹果");
                break;
            case 1:
                toFragment = fragmentManager.findFragmentByTag("news");
                toolbar.setTitle("新闻资讯");
                break;
            case 2:
                toFragment = fragmentManager.findFragmentByTag("video");
                toolbar.setTitle("视频");
                break;
            case 3:
                toFragment = fragmentManager.findFragmentByTag("weather");
                toolbar.setTitle("天气");
                break;
        }
        fragmentTransaction.hide(currentFragment).show(toFragment).commit();
        currentFragment = toFragment;
    }

    @Override
    public void onBackPressed() {

        if (mDrawerLayout.isDrawerOpen(ll_drawer)) {
            mDrawerLayout.closeDrawer(ll_drawer);
            return;
        }

        long now = System.currentTimeMillis();
        if ((now - firstPressTime) > 2000) {
            Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
            firstPressTime = now;
        } else {
            finish();
            System.exit(0);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.draw_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings5) {
            mDrawerLayout.openDrawer(ll_drawer);
            return true;
        }else{
            Snackbar.make(getWindow().getDecorView(), "我是普通的Snackbar", Snackbar.LENGTH_LONG).show();
        }


        return super.onOptionsItemSelected(item);
    }

}
